package com.ckh.springboot.controller;


import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckh.springboot.common.Constants;
import com.ckh.springboot.common.Result;
import com.ckh.springboot.entity.JoinClass;
import com.ckh.springboot.entity.PleaseLeave;
import com.ckh.springboot.entity.SysClass;
import com.ckh.springboot.service.IJoinClassService;
import com.ckh.springboot.service.IPleaseLeaveService;
import com.ckh.springboot.service.ISysClassService;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ckh
 * @since 2022-04-04
 */
@RestController
@RequestMapping("/api/pleaseLeave")
public class PleaseLeaveController {


    @Resource
    private IPleaseLeaveService pleaseLeaveService;
    @Resource
    private ISysClassService sysClassService;
    @Resource
    private IJoinClassService joinClassService;

    /**
     * 提交请假
     * @return
     */
    @PostMapping("/pleaseLeaveInsert")
    public Result pleaseLeaveInsert(@RequestBody PleaseLeave pleaseLeave) {

        //查出老师创建的班级
        List<SysClass> list = sysClassService.list(Wrappers.<SysClass>lambdaQuery().eq(SysClass::getUserId, pleaseLeave.getTeacherId()));
        if(ObjectUtils.isNotEmpty(list)){
            for (SysClass sysClass : list) {
                List<JoinClass> list1 = joinClassService.list(Wrappers.<JoinClass>lambdaQuery().eq(JoinClass::getClassId, sysClass.getId()));
                if(list1.stream().filter(item->item.getUserId().equals(pleaseLeave.getUserId())).findAny().isPresent()){
                    break;
                }else {
                    return Result.error(Constants.CODE_400, "请先加入该教师班级后请假!");
                }
            }
        }
        return Result.success(pleaseLeaveService.save(pleaseLeave));
//        return Result.success();
    }


    /**
     * 查询请假记录
     * @return
     */
    @GetMapping("/getPleaseLeave")
    public Result getPleaseLeave(@RequestParam Integer userId) {
        List<PleaseLeave> list = pleaseLeaveService.list(Wrappers.<PleaseLeave>lambdaQuery().eq(PleaseLeave::getTeacherId, userId)
                .or()
                .eq(PleaseLeave::getUserId, userId));
        return Result.success(list);
    }

    /**
     * 审批请假
     * @return
     */
    @GetMapping("/updateStatus")
    public Result updateStatus(@RequestParam Integer leaveId,@RequestParam Integer status) {
        PleaseLeave pleaseLeave = new PleaseLeave();
        pleaseLeave.setId(leaveId);
        pleaseLeave.setStatus(status);
        return Result.success(pleaseLeaveService.updateById(pleaseLeave));
    }


    /**
     * 后台请假列表
     * @return
     */
    @GetMapping("/getPleaseLeaveList")
    public Result getPleaseLeaveList(String name,
                                     @RequestParam Integer pageNum,
                                     @RequestParam Integer pageSize) {
        Page<PleaseLeave> pleaseLeaveList = pleaseLeaveService.getPleaseLeaveList(new Page<>(pageNum, pageSize), name);
        return Result.success(pleaseLeaveList);
    }

    /**
     * 后台请假列表删除
     * @return
     */
    @PostMapping("/deletePleaseLeave")
    public Result deletePleaseLeave(@RequestBody PleaseLeave pleaseLeave) {
        return Result.success(pleaseLeaveService.removeById(pleaseLeave));
    }

}

